<template>
    <div>
         <!--面包屑功能-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
      </el-breadcrumb>
      <!--卡片区-->
      <el-card>
        <el-row :gutter="20">
            <el-col :span="10">
                <el-input placeholder="请输入内容"
                v-model="queryInfo.query" class="input-with-select" clearable @clear="getGoodsList">
                   <template slot="prepend">商品名称</template>
                    <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
                  </el-input>
            </el-col>
            <el-col :span="5">
                <el-button type="default" @click="openAddGoods">增加商品</el-button>
            </el-col>
        </el-row>
        <!--表格部分-->
        <el-table
        border
        stripe
        :data="goodList"
        style="width: 100%">
        <el-table-column type="index">
        </el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名称">
        </el-table-column>
        <el-table-column
          prop="goods_price"
          width="150"
          label="商品价格(元）">
        </el-table-column>
        <el-table-column
        prop="goods_weight"
        width="100"
        label="商品重量">
      </el-table-column>
      <el-table-column
      width="150"
      label="创建时间">
      <template slot-scope="scope">
        {{scope.row.add_time | dateFormat}}
      </template>
    </el-table-column>
    <el-table-column
    label="操作"
    width="200">
    <template slot-scope="scope">
      <el-button @click="handleClick(scope.row)"
      type="success" size="mini" icon="el-icon-delete">删除</el-button>
      <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
    </template>
  </el-table-column>
      </el-table>
      <!--分页区放置-->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </el-card>
    </div>
</template>

<script>
export default {
  data() {
    return {
      // 条件查询数据对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 查询商品数据列表
      goodList: [],
      // 查询出来记录总条数
      total: 10
    }
  },
  // 定义vue中方法
  methods: {
    // 1 查询商品列表
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.warning('获取商品列出错')
      }
      // 设置到集合中
      this.goodList = res.data.goods
      this.total = res.data.total
    },
    // 分页控件相关事件对应方法
    // 每页条数变化
    handleSizeChange(val) {
      this.queryInfo.pagesize = val
      // 再查询
      this.getGoodsList()
    },
    // 当前页码变化
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val
      this.getGoodsList()
    },
    // 打开增加的部分
    openAddGoods() {
      this.$router.push('/AddGood')
    }
  },
  // vue生命周期,创建成功时
  created() {
    this.getGoodsList()
  }
}
</script>

<style scoped lang="less">
.input-with-select{
    background-color: #fff;
}
</style>
